package com.sky.service.impl;

import com.sky.dto.GoodsSalesDTO;
import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.ReportMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.invoke.MethodType;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Period;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private ReportMapper reportMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UserMapper userMapper;

    /**
     * 营业额统计
     */
    @Override
    public TurnoverReportVO turnoverStatistics(LocalDate begin, LocalDate end) {

        //日期字符串
        List<LocalDate> dateList = getDates(begin, end);

        List<Double> turnoverList = new ArrayList<>();

        for (LocalDate localDate : dateList) {
            //获取开始时间
            LocalDateTime localDateTime = LocalDateTime.of(localDate, LocalTime.MIN);
            //获取结束时间
            LocalDateTime localDateTime1 = LocalDateTime.of(localDate, LocalTime.MAX);

            turnoverList.add(orderMapper.sumByMap(localDateTime, localDateTime1, Orders.COMPLETED) == null ? 0.0 : orderMapper.sumByMap(localDateTime, localDateTime1, Orders.COMPLETED));

        }
        return TurnoverReportVO
                .builder()
                .dateList(StringUtils.join(dateList, ","))//日期集合字符串
                .turnoverList(StringUtils.join(turnoverList, ","))//营业额集合字符串
                .build();
    }


    /**
     * 获取指定时间区间内的日期列表
     * @param begin
     * @param end
     * @return
     */
    private static List<LocalDate> getDates(LocalDate begin, LocalDate end) {
        //存放 日期
        List<LocalDate> dateList = new ArrayList<>();
        dateList.add(begin);
        while (!begin.equals(end)) {
            begin = begin.plusDays(1);
            //添加到集合中
            dateList.add(begin);
        }
        return dateList;
    }

    /**
     * 用户统计
     *
     * @param begin
     * @param end
     * @return
     */
    @Override
    public UserReportVO userStatistics(LocalDate begin, LocalDate end) {
        //获取日期列表
        List<LocalDate> dateList = getDates(begin, end);

        //获取新用户列表
        List<Integer> totalUserList = new ArrayList<>();
        //获取总用户数
        List<Integer> newUserList = new ArrayList<>();
        for (LocalDate localDate : dateList) {
            //获取开始时间
            LocalDateTime begintime = LocalDateTime.of(localDate, LocalTime.MIN);
            //获取结束时间
            LocalDateTime endtime = LocalDateTime.of(localDate, LocalTime.MAX);
            totalUserList.add(userMapper.countByDate( begintime, endtime));
            newUserList.add(userMapper.countByDate( begintime, begintime));
        }
        return UserReportVO.builder()
                .dateList(StringUtils.join(dateList, ","))
                .totalUserList( StringUtils.join(totalUserList, ","))
                .newUserList( StringUtils.join(newUserList, ","))
                .build();
    }

    @Override
    public OrderReportVO ordersStatistics(LocalDate begin, LocalDate end) {
        //获取日期列表
        List<LocalDate> dateList = getDates(begin, end);

        //获取订单列表
        List<Integer> validOrderCountList = new ArrayList<>();
        //获取有效订单数量
        List<Integer> orderCountList = new ArrayList<>();

        for (LocalDate date : dateList) {
            //获取开始时间
            LocalDateTime begintime = LocalDateTime.of(date, LocalTime.MIN);
            //获取结束时间
            LocalDateTime endtime = LocalDateTime.of(date, LocalTime.MAX);
            validOrderCountList.add(orderMapper.OrdercountByDate( begintime, endtime,Orders.COMPLETED));
            orderCountList.add(orderMapper.OrdercountByDate( begintime, endtime,null));
        }

        // 获取总订单数
        //Integer totalOrderCount = orderMapper.OrdercountByDate(null, LocalDateTime.now(),null);
        Integer totalOrderCount = orderCountList.stream().reduce(Integer::sum).get();//求和避免数据库读写

        // 获取有效订单数
        //Integer validOrderCount = orderMapper.OrdercountByDate(null, LocalDateTime.now(),Orders.COMPLETED);
        Integer validOrderCount = validOrderCountList.stream().reduce(Integer::sum).get();//求和避免数据库读写


        Double orderCompletionRate = 0.0;
        if (totalOrderCount != 0) {
            orderCompletionRate  = validOrderCount.doubleValue() / totalOrderCount;
        }
        return OrderReportVO.builder()
                .dateList(StringUtils.join(dateList, ","))
                .orderCountList(StringUtils.join(orderCountList, ","))
                .validOrderCountList(StringUtils.join(validOrderCountList, ","))
                .totalOrderCount(totalOrderCount)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .build();

    }

    /**
     * 统计指定时间区间内的销量排名top10
     * @param begin
     * @param end
     * @return
     */
    @Override
    public SalesTop10ReportVO top10(LocalDate begin, LocalDate end) {

        //获取开始时间
        LocalDateTime begintime = LocalDateTime.of(begin, LocalTime.MIN);
        //获取结束时间
        LocalDateTime endtime = LocalDateTime.of(end, LocalTime.MAX);
        // 获取top10
        List<GoodsSalesDTO> list = orderMapper.getSalesTop10(begintime, endtime);

        return SalesTop10ReportVO.builder()
                .nameList(list.stream().map(GoodsSalesDTO::getName).collect(Collectors.joining(",")))
                .numberList(list.stream().map(GoodsSalesDTO::getNumber).map(num -> num.toString()).collect(Collectors.joining(",")))
                .build();
    }
}
